package com.dominator.controller.backend;

import com.dominator.mybatis.dao.T_userPO;
import com.dominator.service.CommonService;
import com.dominator.utils.exception.ApiException;
import com.dominFramework.core.typewrap.Dto;
import com.dominFramework.core.utils.JsonUtils;
import com.dominator.enums.ReqEnums;
import com.dominator.utils.api.ApiMessage;
import com.dominator.utils.api.ApiUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;


@RestController
@RequestMapping("/backend/v1/common")
public class CommonController {

	private final static Logger log = LoggerFactory.getLogger(CommonController.class);

	@Autowired
	private CommonService commonService;

	/**
	 * 发手机验证码
	 * @param paramStr phone String 必传 手机号
	 *
	 * @return
	 */
	@PostMapping("/sendMessage")
	public ApiMessage sendMessage(@RequestParam String paramStr) {
		ApiMessage msg;
		try {
			Dto dto = JsonUtils.toDto(paramStr);
			ApiUtils.checkParam(dto, "phone");
			msg = commonService.sendMessage(dto);
		} catch (ApiException e) {
			msg = new ApiMessage(e.getCode(), e.getMessage());
		} catch (Exception e) {
			msg = new ApiMessage(ReqEnums.SYS_ERROR.getCode(), ReqEnums.SYS_ERROR.getMsg());
			e.printStackTrace();
		}
		return msg;
	}

	/**
	 * 校验手机验证码
	 * @param paramStr phone String 必传 手机号
	 *                 code String 必传 验证码
	 * @return
	 */
	@GetMapping("/checkMessage")
	public ApiMessage checkMessage(@RequestParam String paramStr) {
		ApiMessage msg;
		try {
			Dto dto = JsonUtils.toDto(paramStr);
			ApiUtils.checkParam(dto, "phone,code");
			msg = commonService.checkMessage(dto);
		} catch (ApiException e) {
			msg = new ApiMessage(e.getCode(), e.getMessage());
		} catch (Exception e) {
			msg = new ApiMessage(ReqEnums.SYS_ERROR.getCode(), ReqEnums.SYS_ERROR.getMsg());
			e.printStackTrace();
		}
		return msg;
	}
}
